// Settings.cpp : implementation file
//

#include "stdafx.h"
#include "NXTPocketPC.h"
#include "Settings.h"
#include "NXTPocketPCDlg.h"


// CSettings dialog

IMPLEMENT_DYNAMIC(CSettings, CDialog)

CSettings::CSettings(CWnd* pParent /*=NULL*/)
	: CDialog(CSettings::IDD, pParent)
	, m_iComPort(0)
	, m_iLeftMotor(0)
	, m_iRightMotor(0)
	, m_iFireMotor(0)
{

}

CSettings::~CSettings()
{
}

void CSettings::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBOX_COMPORT, m_comBoxComPort);
	DDX_CBIndex(pDX, IDC_COMBOX_COMPORT, m_iComPort);
	DDV_MinMaxInt(pDX, m_iComPort, 0, 20);
	DDX_CBIndex(pDX, IDC_COMBOX_LEFT_MOTOR, m_iLeftMotor);
	DDX_CBIndex(pDX, IDC_COMBOX_RIGHT_MOTOR, m_iRightMotor);
	DDX_CBIndex(pDX, IDC_COMBOX_FIRE_MOTOR, m_iFireMotor);
}


BEGIN_MESSAGE_MAP(CSettings, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CSettings::OnBnClickedButtonSave)
END_MESSAGE_MAP()


BOOL CSettings::OnInitDialog()
{
	DWORD iComPort,iMotorPort;

	CDialog::OnInitDialog();

	m_RegKey.Create( HKEY_CURRENT_USER,_T("Software\\NXTPocketPC")); 

	if(!m_RegKey.QueryValue(iComPort,_T("ComPort"))==ERROR_SUCCESS)
		iComPort = 1;

	m_iComPort = iComPort;


	if(!m_RegKey.QueryValue(iMotorPort,_T("LeftMotor"))==ERROR_SUCCESS)
		iMotorPort = 0;

	m_iLeftMotor = iMotorPort;


	if(!m_RegKey.QueryValue(iMotorPort,_T("RightMotor"))==ERROR_SUCCESS)
		iMotorPort = 1;

	m_iRightMotor = iMotorPort;


	if(!m_RegKey.QueryValue(iMotorPort,_T("FireMotor"))==ERROR_SUCCESS)
		iMotorPort = 2;

	m_iFireMotor = iMotorPort;

	UpdateData(false);


	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


// CSettings message handlers


void CSettings::OnBnClickedButtonSave()
{
	if(UpdateData(true))
	{
		m_RegKey.SetValue(m_iComPort,_T("ComPort"));
		m_RegKey.SetValue(m_iLeftMotor,_T("LeftMotor"));
		m_RegKey.SetValue(m_iRightMotor,_T("RightMotor"));
		m_RegKey.SetValue(m_iFireMotor,_T("FireMotor"));
	}
}


